<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/14
 * author : weblinuxgame
 * filename: helper.php
 */

use App\Core\Enums\Define;
use Illuminate\Contracts\Support\Arrayable;
use Symfony\Component\HttpFoundation\Cookie as CookieItem;

if (!function_exists('config_path')) {
    /**
     * Get the path to the storage folder.
     *
     * @param string $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->getConfigurationPath($path);
    }
}

if (!function_exists('array_copy')) {
    function array_copy($src): array
    {
        if (is_array($src)) {
            return $src;
        }
        $dest = [];
        if (is_object($src)) {
            if ($src instanceof ArrayAccess) {
                foreach ($src as $key => $value) {
                    $dest[$key] = $value;
                }
            } else if ($src instanceof Arrayable) {
                $dest = $src->toArray();
            }
        }
        return $dest;
    }
}

if (!function_exists('cookieItem')) {
    /**
     * cookie Data
     * @param $key
     * @param $value
     * @param $expire
     * @param array $options
     * @return CookieItem
     */
    function cookieItem($key, $value, $expire, array $options = [])
    {
        if (!is_numeric($expire) && is_string($expire)) {
            $expire = config($expire, 3600);
        }
        if (is_numeric($expire) && time() > $expire && $expire > 0) {
            $expire = time() + $expire;
        }
        return new CookieItem($key, $value, $expire,
            $options['path'] ?? '/', $options['domain'] ?? null,
            false, false, false
        );
    }
}

